/*  dreic -- a compiler for Drei
 *
 *  $Id: PrinterTest.scala 2976 2006-10-18 14:34:08Z mihaylov $
 */

package dreic;


/**
 * A small test class for the pretty printer of dreic; it reads in a
 * file and prints out the abstract syntax tree. If the program is
 * erroneous, the compiler will print a corresponding error message
 * instead.
 *
 * usage: scala dreic.PrinterTest <file.drei>
 */
object PrinterTest {
  import java.io.{FileInputStream, FileWriter, PrintWriter, IOException}

  def main(args: Array[String]): Unit = {
    if (args.length == 0) {
      Report.fail("usage: scala dreic.PrinterTest <file.drei>")
    }
    try {
      val in = new FileInputStream(args(0))
      val tree = new Parser(in).parse
      in.close()
      if (Report.errors == 0) {
        val out = if (args.length < 2) new PrintWriter(System.out)
                  else new PrintWriter(new FileWriter(args(1)))
        new Printer(out).print(tree)
        out.close()
      }
      Report.exit()
    }
    catch {
      case e: IOException => Report.fail(e.getMessage())
    }
  }
}
